import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zchope_student/utils/mydio.dart';
import 'package:zchope_student/utils/utils.dart';
import 'package:zchope_student/view/textfield.dart';

import '../apis.dart';
import '../const.dart';

class ChangePass extends StatefulWidget {
  @override
  _ChangePassState createState() => _ChangePassState();
}

class _ChangePassState extends State<ChangePass> {
  Map<String, dynamic> data = Map();
  var _scaffoldkey = new GlobalKey<ScaffoldState>();



  @override
  void initState() {
    getBh();
  }

  void getBh() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    data['memberBh'] = sp.getString("bh");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Const.black,
        key: _scaffoldkey,
        appBar: AppBar(
          title: Text('修改密码'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 25, vertical: 10),
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 10,
                  ),
                  PassField(
                    labelText: '请输入旧密码',
                    map: data,
                    mapKey: 'oldPasswd',
                  ),
//                  SizedBox(
//                    height: 10,
//                  ),
                  PassField(
                    labelText: '请输入新密码',
                    map: data,
                    mapKey: 'newPasswd',
                  ),
//                  SizedBox(
//                    height: 10,
//                  ),
                  PassField(
                    labelText: '请再输入新密码',
                    map: data,
                    mapKey: 'newRePasswd',
                  ),

                ],
              ),
            ),
            MaterialButton(
              height: 68,
              minWidth: MediaQuery.of(context).size.width,
              elevation: 0,
              onPressed: () {
                if (data['oldPasswd'] == null) {
                  Utils.getInstance().showSnackBar(_scaffoldkey,'旧密码不能为空');
                  return;
                }
                if (data['newPasswd'] == null) {
                  Utils.getInstance().showSnackBar(_scaffoldkey,'新密码不能为空');
                  return;
                }
                if (data['newPasswd'] != data['newRePasswd']) {
                  Utils.getInstance().showSnackBar(_scaffoldkey,'两次输入不一致');
                  return;
                }

                data.remove('newRePasswd');
                MyDio.getInstance().post(Apis.pwd_change, (data) async {
                  print(data);
                  Utils.getInstance().showSnackBar(_scaffoldkey,data['msg']);
                  if (data['resultCode'] == 0) {
                    Navigator.pop(context);
                  }
                }, params: data);
              },
              child: Text(
                "确认修改",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              color: Const.gold,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(30),
                      bottomRight: Radius.circular(30))),
            )
          ],
        ),
    );
  }

  @override
  void dispose() {
    MyDio.getInstance().clearInstance();
  }
}
